package com.chengyu.stock.service.impl;

import com.chengyu.stock.constant.StockConstant;
import com.chengyu.stock.mapper.SysUserMapper;
import com.chengyu.stock.pojo.entity.SysUser;
import com.chengyu.stock.service.UserService;
import com.chengyu.stock.utils.IdWorker;
import com.chengyu.stock.vo.req.LoginReqVo;
import com.chengyu.stock.vo.resp.LoginRespVo;
import com.chengyu.stock.vo.resp.R;
import com.chengyu.stock.vo.resp.ResponseCode;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    /**
     * 分布式环境保证生成id唯一
     */
    @Autowired
    private IdWorker idWorker;

    @Override
    public SysUser getUserByUserName(String userName) {
        return sysUserMapper.getUserByUserName(userName);
    }

    //用户登录功能
    @Override
    public R<LoginRespVo> login(LoginReqVo vo) {
        //校验参数合法性
        if (vo == null || StringUtils.isBlank(vo.getUsername()) || StringUtils.isBlank(vo.getPassword())) {
            return R.error(ResponseCode.DATA_ERROR);
        }
        //校验验证码和sessionId是否有效
        if (StringUtils.isBlank(vo.getCode()) || StringUtils.isBlank(vo.getRKey())) {
            return R.error(ResponseCode.DATA_ERROR);
        }
        String rCode = (String) redisTemplate.opsForValue().get(StockConstant.CHECK_PREFIX + vo.getRKey());
        if (StringUtils.isBlank(rCode) || !rCode.equalsIgnoreCase(vo.getCode())) {
            return R.error(ResponseCode.CHECK_CODE_ERROR);
        }
        redisTemplate.delete(StockConstant.CHECK_PREFIX + vo.getRKey());
        //根据用户名查询获取用户信息
        SysUser dbUser = sysUserMapper.getUserByUserName(vo.getUsername());
        if (dbUser == null) {
            return R.error(ResponseCode.ACCOUNT_NOT_ERROR);
        }
        if (!passwordEncoder.matches(vo.getPassword(), dbUser.getPassword())) {
            return R.error(ResponseCode.USERNAME_OR_PASSWORD_ERROR);
        }
        LoginRespVo respVo = new LoginRespVo();
        BeanUtils.copyProperties(dbUser, respVo);
        return R.ok(respVo);
    }

    @Override
    public R<Map> getCaptchaCode() {
        //生成sessionId和随机校验码
        String sessionId = String.valueOf(idWorker.nextId());
        String checkCode = RandomStringUtils.randomAlphanumeric(4);
        //存储到redis下
        redisTemplate.opsForValue().set(StockConstant.CHECK_PREFIX + sessionId, checkCode, 1, TimeUnit.MINUTES);
        //响应
        HashMap<String, Object> info = new HashMap<>();
        info.put("code", checkCode);
        info.put("rkey", sessionId);
        return R.ok(info);
    }
}
